#include <iostream>
#include <vector>
#include <map>
using namespace std;


class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        int* vocab = new int[26];
        for(int j = 0;j<26;j++){
            vocab[j] = 0;
        }
        std::map<std::string ,vector<string> > dict;
        for(int i = 0;i<strs.size();i++){
            for(int j = 0;j<strs[i].size();j++){
                vocab[strs[i][j]-'a']++;
            }
            std::string str = "";
            for(int j = 0;j<26;j++){
                str += ('a'+j)+vocab[j];
                vocab[j] = 0;
            }
            if(dict.find(str)==dict.end()){
                vector<std::string> vec;
                vec.push_back(strs[i]);
                dict[str] = vec;
            } else{
                dict[str].push_back(strs[i]);
            }
        }
        vector<vector<string>> result;
        for(std::map<std::string ,vector<string> >::iterator it = dict.begin();it!=dict.end();it++){
            result.push_back(it->second);
        }
        delete[] vocab;
        return result;
    }
};

int main() {
    Solution solution;
//    vector<std::string> strs = {"eat","tea","tan","ate","nat","bat"};
//    vector<vector<string>> result = solution.groupAnagrams(strs);
//    for(int i = 0;i<result.size();i++){
//        for(int j = 0;j<result[i].size();j++){
//            std::cout<<result[i][j]<<" ";
//        }
//        std::cout<<std::endl;
//    }
    return 0;
}

